<?php 
	$_SESSION['realpage'] = true;
?>

<?php 
// includes
require_once "./class.GeneralServices.php";

session_start();

function spoofing()
{
	if ($_POST['argler'])
	{
		if ($_POST['argler'] == $_SESSION['spoof_preventer'])
		{
			return  true;
		}	
	}
		
	return false;
}
?>
<!DOCTYPE HTML>
<html>
<head>
<?php require_once "./HtmlIncludes.php";?>
<title>Insert title here</title>
</head>
    <body onload="load()">
    <script type="text/javascript">       
		function load()
		{
			var ur = window.parent.document.URL;			
			if(ur.substring(ur.length - 15, ur.length) != "Pages/panes.php")
				window.location.replace("./panes.php");
			GetUserGroups(5, 0);
		}

    function SearchGroup(creteria, limit)
    {
    	if(creteria != null && creteria != '')
    	{
    		if (window.XMLHttpRequest)
    		{// code for IE7+, Firefox, Chrome, Opera, Safari
    			xmlhttp=new XMLHttpRequest();
    		}
    		else
    		{// code for IE6, IE5
    			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    		}
    		xmlhttp.onreadystatechange=function()
    		{
    			if (xmlhttp.readyState==4 && xmlhttp.status==200)
    		    {
    			    if(xmlhttp.responseText == 'false')
    			    {
    					alert('false');
    			    }
    			    else
    			    {
    				    if(xmlhttp.responseText != "")
    				    {
    				    	$('#groupTabSearchResults').css('visibility', 'visible');
    				    	$('#groupTabSearchResults').css('display', 'block');
    				    	$('#groupTabSearchResults').html(xmlhttp.responseText);
    				    }else
    				    {
    				    	$('#groupTabSearchResults').css('visibility', 'visible');
    				    	$('#groupTabSearchResults').css('display', 'block');
    				    	$('#groupTabSearchResults').html('Oops! We could not find any groups...');
    				    }
    			    }
    			}
    		};
    		xmlhttp.open("GET","./ajax.php?method=searchGroup&sid="+"<?php echo session_id(); ?>"+"&creteria="+creteria+"&limit="+limit+"&uid="+$('#TabUID').val(),true);
    		xmlhttp.send();
    	}
    }

    function CreateGroup(gname, gdesc, gvisibility)
    {
    	if(gname != '' && gdesc != '')
    	{
    		if (window.XMLHttpRequest)
    		{// code for IE7+, Firefox, Chrome, Opera, Safari
    			xmlhttp=new XMLHttpRequest();
    		}
    		else
    		{// code for IE6, IE5
    			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    		}
    		xmlhttp.onreadystatechange=function()
    		{
    			if (xmlhttp.readyState==4 && xmlhttp.status==200)
    		    {
    			    if(xmlhttp.responseText == 'false')
    			    {
    					alert('false');
    			    }
    			    else
    			    {    				    
    			    	setGroupId(xmlhttp.responseText);
    			    }
    			}
    		};	
    		xmlhttp.open("GET","./ajax.php?method=createGroup&sid="+"<?php echo session_id(); ?>"+"&name="+gname+"&desc="+replaceBR(gdesc)+"&visibility="+gvisibility+"&uid="+$('#TabUID').val(),true);
    		xmlhttp.send();
    	}
    }

    function GetUserGroups(limit, downPour)
    {
    		if (window.XMLHttpRequest)
    		{// code for IE7+, Firefox, Chrome, Opera, Safari
    			xmlhttp=new XMLHttpRequest();
    		}
    		else
    		{// code for IE6, IE5
    			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    		}
    		xmlhttp.onreadystatechange=function()
    		{
    			if (xmlhttp.readyState==4 && xmlhttp.status==200)
    		    {
    			    if(xmlhttp.responseText == 'false')
    			    {
    					alert('false');
    			    }
    			    else
    			    {
    				    $('#UsersGroups').html(xmlhttp.responseText);
    			    }
    			}
    		};	
    		xmlhttp.open("GET","./ajax.php?method=getUsersGroups&sid="+"<?php echo session_id(); ?>"+"&limit="+limit+"&downPour="+downPour+"&uid="+$('#TabUID').val(),true);
    		xmlhttp.send();
    	}
    
    function ShowMoreGroups(limit, downPour)
    {
    		if (window.XMLHttpRequest)
    		{// code for IE7+, Firefox, Chrome, Opera, Safari
    			xmlhttp=new XMLHttpRequest();
    		}
    		else
    		{// code for IE6, IE5
    			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    		}
    		xmlhttp.onreadystatechange=function()
    		{
    			if (xmlhttp.readyState==4 && xmlhttp.status==200)
    		    {
    			    if(xmlhttp.responseText == 'false')
    			    {
    					alert('false');
    			    }
    			    else
    			    {
        			    if($('#lastGroupShown').val() != null && xmlhttp.responseText != "")
        			    {
    			    		$('#lastGroupShown').remove();
        			    }
    				    $('#UsersGroups').append(xmlhttp.responseText);
    				    if(xmlhttp.responseText == "")
    				    {
    				    	$('#NoMoreGroupsError').css('visibility', 'visible');
    				    	$('#NoMoreGroupsError').css('display', 'block');
    				    	$('#showMoreGroups').css('visibility', 'hidden');
    				    	$('#showMoreGroups').css('display', 'none');    				    	    				    	
    				    }
    			    }
    			}
    		};	
    		xmlhttp.open("GET","./ajax.php?method=getMoreGroups&sid="+"<?php echo session_id(); ?>"+"&limit="+limit+"&downPour="+downPour+"&uid="+$('#TabUID').val(),true);
    		xmlhttp.send();
    	}

    function setGroupId(gid)
    {
    	if(gid != null)
    	{
    		if (window.XMLHttpRequest)
    		{// code for IE7+, Firefox, Chrome, Opera, Safari
    			xmlhttp=new XMLHttpRequest();
    		}
    		else
    		{// code for IE6, IE5
    			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    		}
    		xmlhttp.onreadystatechange=function()
    		{
    			if (xmlhttp.readyState==4 && xmlhttp.status==200)
    		    {
    			    if(xmlhttp.responseText == 'false')
    			    {
    					alert('false');
    			    }
    			    else
    			    {
    			    	ShowGroupTab(gid);
    			    }
    			}
    		};
    		xmlhttp.open("GET","./ajax.php?method=setGid&sid="+"<?php echo session_id(); ?>"+"&gid="+gid,true);
    		xmlhttp.send();
    	}
    }
    </script>
    	<div class="PaneHeader">
    		Manage Your Groups
    	</div>  
    	
    	
    	<div id="tabs">
	<ul>
		<li><a href="#tabs-1">Search A Group</a></li>
		<li><a href="#tabs-2">User's Groups</a></li>
		<li><a href="#tabs-3">Create a group</a></li>
	</ul>
	<div id="tabs-1">
		<div style="float:left;" onclick="OpenHiddenDiv('searchGroupDiv');"><strong>Search A Group:</strong></div>
			<br>
			<input type="hidden" id="TabUID" value="<?php echo $_SESSION['userObj']->uid;?>"/>
			<div style="width: 170px;">
				<span style="float: left;"><input id="groupsTabText" type="text" value="Search for a group" onmouseover="RemoveText('groupsTabText', 'Search for a group')" onmouseout="ReturnText('groupsTabText', 'Search for a group')" onkeypress="enterPressed(event)"/></span>
			    <div class="ui-state-default ui-corner-all" onclick="$('#groupTabSearchResults').html(''); $('#groupsTabText').val(''); ReturnText('groupsTabText', 'Search for a group');"><span id="CloseSearchGroupTabButton">X</span></div>
			    <div id="groupTabSearchResults" style="visibility: hidden; display: none;"></div>
			    <br>
			</div>
		    <script type="text/javascript">
		    function enterPressed(e)
		    {
		    	if(e.keyCode == 13)
		    	{
		    		SearchGroup($('#groupsTabText').val(), 4);		    	
		    	}
		    }
		    </script>								
	</div>
	<div id="tabs-2">
		<div style="float:left;"><strong>User's Groups:</strong></div>
		<br>				
		<div id="UsersGroups">
		<ul>
		
		</ul>
		</div>		
		<span id="showMoreGroups" onclick="ShowMoreGroups(5,$('#lastGroupShown').val())">Show more...</span>
		<div id="NoMoreGroupsError" style="visibility: hidden; display: none; color: red;">There are no more groups!</div>				
	</div>
	<div id="tabs-3">
		<div style="float:left;" onclick="OpenHiddenDiv('createGroupDiv');"><strong>Create a group:</strong></div>
			<br>
				<br>
				<form action="" METHOD=POST>
					<span>Name:</span><span style="margin-left: 42px;"><input id="creatGroup_GroupName" type="text" class="text ui-widget-content ui-corner-all"/></span>
					<br>
					<span style="vertical-align: top;">Description:</span><span style="margin-left: 4px;"><textarea id="creatGroup_GrouDescription" cols="14" rows="4" class="text ui-widget-content ui-corner-all"></textarea></span>					
					<br>									
					<span>Public:</span><input id="creatGroup_GroupVisibility" type="checkbox" style="margin-left: 46px;" class="text ui-widget-content ui-corner-all"/>										
					<br>
					<span id="CreateGroupButton" style="margin-left: 210px;" onclick="CreateGroup($('#creatGroup_GroupName').val(), $('#creatGroup_GrouDescription').val(), $('#creatGroup_GroupVisibility').is(':checked'))">Create Group</span>
				</form>
	</div>
</div>	
<script type="text/javascript">
$( "#tabs" ).tabs({event: "mouseover"});
$('#CloseSearchGroupTabButton').button();
$('#CreateGroupButton').button();
$('#showMoreGroups').button();
</script>	
    </body>
</html>
<?php 
	$_SESSION['realpage'] = false;
?>